home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / clchat413.lha / CLChat413 / ChatBot / source / faqmanager.c < prev    next >
C/C++ Source or Header  |  1995-10-25  |  3KB  |  153 lines

  1. #include <proto/dos.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. //
  6. //    Usage: FAQ dir VIEW|ADD|DEL [topic] [addtext|delfaq nr]
  7. //
  8.  
  9. #define INVCHAR ":/ \"\\"
  10.  
  11. #include "rev.h"
  12. char version[]á= {á"$VER: CLChat-ChatBot-Faqmanager " VERTAG };
  13.  
  14. static int exists( char *path )
  15. {
  16.     BPTR l = Lock( path, SHARED_LOCK );
  17.     if( l )
  18.         UnLock( l );
  19.     return( (int)l);
  20. }
  21.  
  22. __aligned struct FileInfoBlock fib;
  23.  
  24. int main( int argc, char **argv )
  25. {
  26.     char faqdir[á256 ];
  27.     char buffer[á256 ];
  28.     int c, d, rc;
  29.     BPTR l, f;
  30.  
  31.     if( argc < 3 )
  32.         exit( 20 );
  33.  
  34.     strcpy( faqdir, argv[á1 ]á);
  35.  
  36.     if( !exists( faqdir ) )
  37.     {
  38.         Printf( "Error; FAQ directory \"%s\" doesn't exist!\n", faqdir );
  39.         return( 20 );
  40.     }
  41.  
  42.     if( !stricmp( argv[á2 ], "ADD" ) )
  43.     {
  44.         if( argc != 5 )
  45.         {
  46.             PutStr( "USAGE: AddFAQ topic \"text\"\n" );
  47.             exit( 5 );
  48.         }
  49.  
  50.         if( strpbrk( argv[á3 ], INVCHAR ) )
  51.         {
  52.             PutStr( "Invalid topic name!\n" );
  53.             exit( 5 );
  54.         }
  55.  
  56.         Printf( "Adding FAQ on topic %s...\n", argv[á3 ]á);
  57.  
  58.         AddPart( faqdir, argv[ 3 ], 256 );
  59.         UnLock( CreateDir( faqdir ) );
  60.  
  61.         for( c = 0; c < 32; c++ )
  62.         {
  63.             sprintf( buffer, "%s/FAQ.%ld", faqdir, c );
  64.             if( !exists( buffer ) )
  65.             {
  66.                 BPTR f = Open( buffer, MODE_NEWFILE );
  67.                 if( f )
  68.                 {
  69.                     Write( f, argv[á4 ], strlen( argv[á4 ] )á);
  70.                     Close( f );
  71.                     Printf( "FAQ on topic %s saved.\n", argv[á3 ] );
  72.                     exit( 0 );
  73.                 }
  74.             }
  75.         }
  76.         Printf( "Sorry, can't save faq, maximum number reached.\n" );
  77.     }
  78.     else if( !stricmp( argv[á2 ], "DEL" ) )
  79.     {
  80.         if( argc != 5 )
  81.         {
  82.             PutStr( "USAGE: DelFAQ topic number\n" );
  83.             exit( 5 );
  84.         }
  85.         AddPart( faqdir, argv[ 3 ], 256 );
  86.         UnLock( CreateDir( faqdir ) );
  87.  
  88.         c = atoi( argv[á4 ]á);
  89.         sprintf( buffer, "%s/FAQ.%ld", faqdir, c - 1 );
  90.         if( !exists( buffer ) )
  91.         {
  92.             Printf( "There is no FAQ #%ld on topic %s!\n", c, argv[á3 ]á);
  93.             exit( 0 );
  94.         }
  95.         DeleteFile( buffer );
  96.         Printf( "FAQ #%ld on topic %s deleted.\n", c, argv[á3 ]á);
  97.         DeleteFile( faqdir );
  98.     }
  99.     else
  100.     {
  101.         if( argc < 4 )
  102.         {
  103.             BPTR l = Lock( faqdir, SHARED_LOCK );
  104.             Printf( "I have FAQs on the following topics:\n" );
  105.             c = 0;
  106.  
  107.             Examine( l, &fib );
  108.             while( ExNext( l, &fib ) )
  109.             {
  110.                 if( c++ )
  111.                     PutStr( ", " );
  112.                 Printf( "%s", fib.fib_FileName );
  113.             }
  114.             UnLock( l );
  115.             Printf( "\n" );
  116.             exit( 0 );
  117.         }
  118.  
  119.         AddPart( faqdir, argv[ 3 ], 256 );
  120.         l = Lock( faqdir, SHARED_LOCK );
  121.         c = 0;
  122.         if( l )
  123.         {
  124.             Examine( l, &fib );
  125.             while( ExNext( l, &fib ) )
  126.                 c++;
  127.         }
  128.  
  129.         if( !l || !c )
  130.         {
  131.             Printf( "Sorry, there is no FAQ available on topic \"%s\".\n", argv[á3 ]á);
  132.             UnLock( l );
  133.             exit( 5 );
  134.         }
  135.  
  136.         Printf( "I have %ld FAQs on topic \"%s\":\n", c, argv[á3 ]á);
  137.  
  138.         for( d = 1, c = 0; c < 32; c++ )
  139.         {
  140.             sprintf( buffer, "%s/FAQ.%ld", faqdir, c );
  141.             f = Open( buffer, MODE_OLDFILE );
  142.             if( f )
  143.             {
  144.                 rc = Read( f, buffer, 256 );
  145.                 buffer[árc ]á= 0;
  146.                 Printf( "(%ld) %s\n", d++, buffer );
  147.                 Close( f );
  148.             }
  149.         }
  150.         UnLock( l );
  151.     }
  152. }
  153.